<template>
<el-container>
  <el-header style="background-color: white; color: black;">
      <h1 align="center">供货商操作</h1>
  </el-header>
  <el-main>
    <!-- 查询条件表单 -->
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="queryForm.suppliersName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="queryForm.suppliersTel" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="queryForm.suppliersEmail" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData()">查询</el-button>
        <el-button type="primary" @click="addSuppliers()">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="100%" >
      <!-- 列组件 -->
      <!-- prop 对应当前页集合的对象属性-->
      <el-table-column prop="suppliersId" label="编号" width="50"></el-table-column>
      <el-table-column prop="suppliersName" label="姓名" width="50"></el-table-column>
      <el-table-column prop="suppliersSex" label="性别" width="50" :formatter="sex"></el-table-column>
      <el-table-column prop="suppliersTel" label="电话" style="0%"></el-table-column>
      <el-table-column prop="suppliersEmail" label="邮箱" style="0%"></el-table-column>
      <el-table-column prop="suppliersPidName" label="证件" width="80"></el-table-column>
      <el-table-column prop="suppliersPid" label="证件号" width="100"></el-table-column>
      <el-table-column prop="suppliersStartTime" label="签发时间" width="100"></el-table-column>
      <el-table-column prop="suppliersEndTime" label="有效期限" style="0%"></el-table-column>
      <el-table-column label="操作" style="0%">
          <!-- slot-scope 插槽: 用来获取循环的迭代变量-->
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="update(scope.row,scope.$index)">修改</el-button>
            <el-button type="danger" size="mini" @click="deleteSuppliers(scope.row,scope.$index)">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
    @size-change="select"
    @current-change="selectPage"
    :current-page="queryForm.page"
    :page-sizes="[10,15,20]"
    :page-size="queryForm.row"
    :total="total"
    layout="total,sizes,prev,pager,next,jumper"
    >
    </el-pagination>
    <el-dialog :title="title" :visible.sync="isShow" width="50%">
        <el-form :model="suppliersForm" status-icon  ref="suppliersForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="姓名" v-show="suppliersForm.isShow" prop="suppliersName">
                <el-input v-model="suppliersForm.suppliersName" ></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-select v-model="suppliersForm.suppliersSex" placeholder="性别">
                <el-option label="男" value="0"></el-option>
                <el-option label="女" value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="邮箱" prop="suppliersEmail"
                :rules="[
                  { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                  { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                ]"
            >
              <el-input style="width: 300px;" v-model="suppliersForm.suppliersEmail"></el-input>
            </el-form-item>
            <el-form-item label="电话" prop="suppliersTel">
              <el-input v-model="suppliersForm.suppliersTel"></el-input>
            </el-form-item>
            <el-form-item label="证件" v-show="suppliersForm.isShow" prop="suppliersName">
                <el-input v-model="suppliersForm.suppliersPidName" ></el-input>
            </el-form-item>
            <el-form-item label="证件号" v-show="suppliersForm.isShow" prop="suppliersName">
                <el-input v-model="suppliersForm.suppliersPid" ></el-input>
            </el-form-item>
            <el-form-item label="签发时间" prop="suppliersStartTime" v-show="!suppliersForm.isShow">
              <div class="block">
                  <el-date-picker
                    v-model="suppliersForm.suppliersStartTime"
                    type="date"
                    placeholder="选择日期"
                    value-format="yyyy-MM-dd">
                  </el-date-picker>
                </div>
            </el-form-item>
            <el-form-item label="有效时间" prop="suppliersEndTime" v-show="!suppliersForm.isShow">
              <div class="block">
                  <el-date-picker
                    v-model="suppliersForm.suppliersEndTime"
                    type="date"
                    placeholder="选择日期"
                    value-format="yyyy-MM-dd">
                  </el-date-picker>
                </div>
            </el-form-item>
            <el-form-item>
                <el-button @click="submitForm(index)">提交</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
  </el-main>
</el-container>
</template>

<script>
  export default {
      data() {
        return {
          isShow:false,
          tableData: [],
          totalPage: 0, //总页数
          total: 0, //总条数
          title:"修改",
          index:0 ,  //0为修改  1为添加
          queryForm: {
            page: 1,
            row: 10,
            suppliersName:"",
            suppliersTel:"",
            suppliersEmail:"",
          },
          suppliersForm: {
            isShow:false,
            suppliersId:0,
            suppliersName:"",
            suppliersPwd:"",
            suppliersTel:"",
            suppliersEmail:"",
            ensurePwd:"",
            suppliersSex:"",
            suppliersStartTime:"",
            suppliersEndTime:"",
            suppliersPidName:"",
            suppliersPid:""
          },
        }
      },
      methods: {
        loadData() { //加载数据
          var self = this;
          var http = this.$http.post("/api/suppliers/suppliersIndex",this.$qs.stringify(this.queryForm));
          http.then(function(rs) {
            if (rs.data.code == 200) {
              self.tableData = rs.data.data.data;
              self.total = rs.data.data.total;
              self.totalPage = rs.data.data.totalPage;
            }else{
              self.tableData=[];
              self.total=0;
            }
          })
        },
        select(row){ //分页下拉框选中触发的事件，row是当前选中条数
          this.queryForm.row=row;
          //刷新
          this.loadData();
        },
        selectPage(page){//选中页码触发的事件, page就是选中的页码
          this.queryForm.page = page;
          //刷新
          this.loadData();
        },
        sex(row){
           if(row.suppliersSex==0){
            return "男";
           }else{
             return "女";
           }
        },
        update(x,index) { //修改回显
        this.title = "修改"
        this.suppliersForm = x;
        this.suppliersForm.suppliersSex = "";
        this.isShow = true;
        this.index = 0
      },
      deleteSuppliers(x,index) { //删除
        this.suppliersForm = x;
        var self = this
        var http = this.$http.post("api/suppliers/deleteSuppliers",this.$qs.stringify(this.suppliersForm));
        http.then(function(rs){
            if(rs.data.code == 200){
              alert(rs.data.msg)
              location.reload();
            }
        })
      },
      addSuppliers(){
        this.title = "添加"
        this.isShow = true;
        this.suppliersForm={};
        this.suppliersForm.isShow = true;
        this.index = 1
      },
      submitForm(i){
        if(i == 0){
          var self = this
          var http = this.$http.post("/api/suppliers/suppliersUpdate",this.$qs.stringify(this.suppliersForm));
          http.then(function(rs){
              if(rs.data.code == 200){
                alert(rs.data.msg)
                location.reload();
              }else{
                alert(rs.data.msg)
              }
          })
        }else{
          var self = this
          var http = this.$http.post("api/suppliers/addSuppliers",this.$qs.stringify(this.suppliersForm));
          http.then(function(rs){
              if(rs.data.code == 200){
                alert(rs.data.msg)
                location.reload();
              }else{
                alert(rs.data.msg)
              }
          })
        }
      }
      },
      mounted(){
        this.loadData();
      }
    }
</script>

<style>
  .el-table .warning-row {
      background: oldlace;
    }

    .el-table .success-row {
      background: #f0f9eb;
    }
</style>
